#include "player.h"
#include "functions.h"
#include "globals.h"

using namespace std;
using namespace RakNet;

map<RakNetGUID, Player*> Player::Players;

Player::Player(RakNetGUID guid, string name, int x, int y)
{
    Players.insert(pair<RakNetGUID, Player*>(guid, this));
    this->guid = guid;
    this->name = name;

    pos.x = x;
    pos.y = y;
    flag.x = x;
    flag.y = y;
    velX = 0;
    velY = 0;
    movementSpeed = 256;

    combatNpc = NULL;
}

Player::~Player()
{
    Players.erase(this->guid);
}

Player* Player::GetPlayerFromGUID(RakNetGUID guid)
{
    map<RakNetGUID, Player*>::iterator i;
    i = Players.find(guid);

    if (i != Players.end())
        return i->second;

    return NULL;
}

map<RakNetGUID, Player*> Player::GetPlayerList()
{
    return Players;
}

void Player::stop()
{
    flag.x = -1;
    flag.y = -1;

    velX = 0;
    velY = 0;
}

void Player::create_flag(int x, int y)
{
    flag.x = x;
    flag.y = y;
    calculate_vel();
}

void Player::attack(Npc* npc)
{
    //inCombat = npcid;
    combatNpc = npc;
    //output.write("%s incombat: %i", name.c_str(), npc->get_id());
}

void Player::move(unsigned long deltaTicks)
{
    if (pos.x < 64)
    {
        //output->write("Travel west!");
        pos.x = 64;
    }

    if (pos.x + 64 > 2560)
    {
        //output->write("Travel east!");
        pos.x = 2560 - 64;
    }

    if (pos.y < 64)
    {
        //output->write("Travel north!");
        pos.y = 64;
    }

    if (pos.y + 64 > 2688)
    {
        //output->write("Travel south!");
        pos.y = 2688 - 64;
    }

    if (flag.x != -1 && flag.y != -1)
    {
        if (pos.x != flag.x)
        {
            pos.x += (deltaTicks / 1000.f) * velX;

            if (velX > 0)
            {
                if (pos.x > flag.x)
                {
                    pos.x = flag.x;
                }
            }
            else
            {
                if (pos.x < flag.x)
                {
                    pos.x = flag.x;
                }
            }
        }

        if (pos.y != flag.y)
        {
            pos.y += (deltaTicks / 1000.f) * velY;

            if (velY > 0)
            {
                if (pos.y > flag.y)
                {
                    pos.y = flag.y;
                }
            }
            else
            {
                if (pos.y < flag.y)
                {
                    pos.y = flag.y;
                }
            }
        }

        if ((pos.x == flag.x) and (pos.y == flag.y))
        {
            stop();
        }
    }

    //output->write("POS: %f %f", pos.x, pos.y);
}

void Player::calculate_vel()
{
    velX = movementSpeed * (flag.x - pos.x) / distance(pos.x, pos.y, flag.x, flag.y);
    velY = movementSpeed * (flag.y - pos.y) / distance(pos.x, pos.y, flag.x, flag.y);
}

RakNet::RakNetGUID Player::get_guid()
{
    return guid;
}

string Player::get_name()
{
    return name;
}

void Player::set_x(int dest_x)
{
    pos.x = dest_x;
    //update_rect();
}

void Player::set_y(int dest_y)
{
    pos.y = dest_y;
    //update_rect();
}

int Player::x()
{
    return (int)pos.x;
}

int Player::y()
{
    return (int)pos.y;
}
